package fenzhixianjie.tanglian.exer04;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] mat = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                mat[i][j] = sc.nextInt();
            }
        }
        int minCount = Integer.MAX_VALUE;
        int startRow = sc.nextInt() - 1, startCol = sc.nextInt() - 1;
        int endRow = sc.nextInt() - 1, endCol = sc.nextInt() - 1;
        int[][] nextPos = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
        Queue<int[]> queue = new LinkedList<>();
        boolean[][] flag = new boolean[n][m];
        queue.offer(new int[] {startRow, startCol, -1, 0});
        flag[startRow][startCol] = true;
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                int[] top = queue.poll();
                if (top[0] == endRow && top[1] == endCol) {
                    minCount = Math.min(minCount, top[3]);
                }
                for (int i = 0; i < 4; i++) {
                    int nextRow = top[0] + nextPos[i][0];
                    int nextCol = top[1] + nextPos[i][1];
                    if (nextRow >= 0 && nextRow < n && nextCol >= 0 && nextCol < m && !flag[nextRow][nextCol] && mat[nextRow][nextCol] == 0) {
                        if (top[2] == i) {
                            queue.offer(new int[] {nextRow, nextCol, i, top[3]});
                        } else if (top[2] == -1) {
                            queue.offer(new int[] {nextRow, nextCol, i, 0});
                        } else {
                            queue.offer(new int[] {nextRow, nextCol, i, top[3] + 1});
                        }
                        flag[nextRow][nextCol] = true;
                    }
                }
                size--;
            }
        }
        System.out.println(minCount);
    }
}
